package com.example.recall_application.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;

import com.example.recall_application.mvp.presenter.MainPresenter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.TreeSet;


public abstract class ResolverHelperImpl<T> implements IResolverHelper {

    protected Handler mHandler;

    protected Uri contentUri;/*读取哪个表的数据*/
    protected String[] mColumns;/*读取哪些列*/
    protected String mCondition;/*限制条件*/
    protected String[] mArgs;/*参数*/
    protected String mSort;/*排序方式*/

    protected T mData;
    protected Cursor mCursor;

    public ResolverHelperImpl(Context context, Handler handler,String condition) {
        mHandler = handler;
        this.mCondition = condition;
        initParams();
        mCursor = query(context);
    }
    public ResolverHelperImpl(Context context, Handler handler) {
        mHandler = handler;
        initParams();
        mCursor = query(context);
    }

    public ResolverHelperImpl(Context context, Handler handler, int type) {
        if(type == 1){
            String time = TimeUtils.getDateBefore(new Date(), Integer.parseInt(MainPresenter.config_p.getP2()));
            mCondition = "date > "+time+" and type = 2";
        }else{
            String time_1 = TimeUtils.getDateBefore(new Date(),Integer.parseInt(MainPresenter.config_p.getP3())/24);
            mCondition = "date > "+time_1+" and type = 1";
        }
        mHandler = handler;
        initParams();
        mCursor = query(context);
    }

    protected abstract void initParams();

    @Override
    public Cursor query(Context context) {
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = null;
        try {
            cursor = resolver.query(contentUri, mColumns, mCondition, mArgs, mSort);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cursor;
    }

    @Override
    public void readData() {
        new Thread() {
            @Override
            public void run() {
                try {
                    parseData();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (mHandler != null) {
                        Message msg = mHandler.obtainMessage();
                        msg.what = getMsgWhat();

                        TreeSet treeSet = new TreeSet((Collection) mData);
                        List tempList = new ArrayList();
                        tempList.addAll(treeSet);
                        msg.obj = tempList;
                        mHandler.sendMessage(msg);
                    }
                    close();
                }
            }
        }.start();
    }

    protected abstract void parseData();

    protected abstract int getMsgWhat();

    @Override
    public void close() {
        if (mCursor != null) {
            try {
                mCursor.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}







































































